package leetcode.code0720;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public String longestWord(String[] words) {
		Arrays.sort(words, ((a, b) -> a.length() == b.length() ? b.compareTo(a) : a.length() - b.length()));
		Set<String> hash = new HashSet<>();
		String ans = "";
		for (String word : words) {
			boolean add = true;
			for (int i = 1; i < word.length(); i++) {
				if (!hash.contains(word.substring(0, i))) {
					add = false;
					break;
				}
			}
			if (add) {
				hash.add(word);
				ans = word;
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare("oiddm", this.longestWord(HelpDebug.str2array2(
				"[\"k\",\"lg\",\"it\",\"oidd\",\"oid\",\"oiddm\",\"kfk\",\"y\",\"mw\",\"kf\",\"l\",\"o\",\"mwaqz\",\"oi\",\"ych\",\"m\",\"mwa\"]")));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

		HelpDebug.compare("yodn", this.longestWord(HelpDebug.str2array2(
				"[\"yo\",\"ew\",\"fc\",\"zrc\",\"yodn\",\"fcm\",\"qm\",\"qmo\",\"fcmz\",\"z\",\"ewq\",\"yod\",\"ewqz\",\"y\"]")));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
